package com.zxl.working.service.impl;

import com.google.gson.Gson;
import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.entity.backend.*;
import com.zxl.working.entity.dto.SellerBillDto;
import com.zxl.working.mapper.backend.InvoiceMapper;
import com.zxl.working.service.InvoiceService;
import com.zxl.working.utils.CommUtil;
import com.zxl.working.utils.HttpUtil;
import com.zxl.working.utils.SerialNoUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    private Logger logger = LogManager.getLogger(InvoiceServiceImpl.class);

    @Resource
    private InvoiceMapper invoiceMapper;

    @Resource
    private SerialNoUtil serialNoUtil;
    @Resource
    private Gson gson;
    @Resource
    private CommUtil commUtil;
    
    @Value("${request.url}")
    private String requestUrl;

    @Autowired
    private HttpUtil httpUtil;
    

    public String qryConsignee(String sellerId,String params){
        List<SellerConsignee> consigneeList = invoiceMapper.qryConsignee(sellerId);

        return commUtil.returnSuccess(consigneeList);
    }

    public String qryTaxType(){
        List<SellerBillTaxType> taxTypes = invoiceMapper.qryTaxType();

        return commUtil.returnSuccess(taxTypes);
    }

    public String qryInvoiceInfo(SellerInfo sellerInfo,String params){
        Map<String,String> paramMap = gson.fromJson(params,Map.class);

        if(paramMap.containsKey("serviceId")){
            ServiceInfo serviceInfo = invoiceMapper.qryServiceInfoById(paramMap.get("serviceId"));
            Map<String,Object> resMap = new HashMap<>();
            resMap.put("sellerInfo",sellerInfo);
            resMap.put("serviceInfo",serviceInfo);

            return commUtil.returnSuccess(resMap);
        }else {
            return commUtil.returnStr(-2,"缺少参数","");
        }
    }

    public String insSellerBill(SysUser sysUser,String params,String sellerName){
        SellerBill sellerBill = gson.fromJson(params,SellerBill.class);
        sellerBill.setBillCode(serialNoUtil.genSerialNo("P",sysUser.getPlatform(),sysUser.getMerchantNumber()));
        sellerBill.setStatus(0);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        invoiceMapper.insSellerBill(sellerBill);
        httpUtil.get(requestUrl+"/backend/dict/sendMain?flag=5&name="+sellerName + "&money="+sellerBill.getTaxableTotal() +"&time="+ formatter.format(new java.util.Date()));
        return commUtil.returnSuccess(sellerBill.getBillCode());
    }

    public String qryBills(String sellerId,String param){
        Map<String,String> qryParam = gson.fromJson(param,Map.class);
        Integer page = Integer.parseInt(qryParam.getOrDefault("page","1"));
        Integer pageSize = Integer.parseInt(qryParam.getOrDefault("pageSize","10"));
        Integer status = Integer.parseInt(qryParam.getOrDefault("status","-1"));
        Integer start = (page - 1)* pageSize;
        List<SellerBillDto> bills = invoiceMapper.qryBills(sellerId,start,pageSize,status);
        Integer cnt = invoiceMapper.qryBillsCnt(sellerId,status);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("details",bills);
        resMap.put("total",cnt);

        return commUtil.returnSuccess(resMap);
    }

    public String qryConsigneeList(String sellerId,String params){
        Map<String,String> qryParam = gson.fromJson(params,Map.class);
        Integer page = Integer.parseInt(qryParam.getOrDefault("page","1"));
        Integer pageSize = Integer.parseInt(qryParam.getOrDefault("pageSize","10"));
        Integer start = (page - 1)* pageSize;

        List<SellerConsignee> consignees = invoiceMapper.qryConsigneeList(sellerId,start,pageSize);
        Integer cnt = invoiceMapper.qryConsigneeCnt(sellerId);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("details",consignees);
        resMap.put("total",cnt);

        return commUtil.returnSuccess(resMap);
    }

    public Integer insConsignee(String sellerId,String params){
        SellerConsignee sellerConsignee = gson.fromJson(params,SellerConsignee.class);
        sellerConsignee.setRecivePersonId(commUtil.getId());
        sellerConsignee.setSellerId(sellerId);

        invoiceMapper.insConsignee(sellerConsignee);

        return 0;
    }

    public Integer updConsignee(String sellerId,String params){
        SellerConsignee sellerConsignee = gson.fromJson(params,SellerConsignee.class);
        sellerConsignee.setSellerId(sellerId);

        invoiceMapper.updConsignee(sellerConsignee);

        return 0;
    }

    public Integer delConsignee(String params){
        Map<String,String> paramMap = gson.fromJson(params,Map.class);

        String id = paramMap.get("recivePersonId");

        invoiceMapper.delConsignee(id);

        return 0;
    }

    public String qryTax(String sellerId,String param){
        Map<String,String> qryParam = gson.fromJson(param,Map.class);
        Integer page = Integer.parseInt(qryParam.getOrDefault("page","1"));
        Integer pageSize = Integer.parseInt(qryParam.getOrDefault("pageSize","10"));
        String sellerName = StringUtils.isBlank(qryParam.getOrDefault("sellerName",null)) ? null : qryParam.getOrDefault("sellerName",null);
        String companyName = StringUtils.isBlank(qryParam.getOrDefault("companyName",null)) ? null : qryParam.getOrDefault("companyName",null);
        String serviceName = StringUtils.isBlank(qryParam.getOrDefault("serviceName",null)) ? null : qryParam.getOrDefault("serviceName",null);
        Integer start = (page - 1)* pageSize;

        List<SellerTax> taxes = invoiceMapper.qryTaxs(sellerId,start,pageSize,sellerName,companyName,serviceName);
        Integer cnt = invoiceMapper.qryTaxsCnt(sellerId,sellerName,companyName,serviceName);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("details",taxes);
        resMap.put("total",cnt);

        return commUtil.returnSuccess(taxes);
    }

    public String qryAccountDetails(String sellerId,String params){
        Map<String,String> qryParam = gson.fromJson(params,Map.class);
        Integer page = Integer.parseInt(qryParam.getOrDefault("page","1"));
        Integer pageSize = Integer.parseInt(qryParam.getOrDefault("pageSize","10"));
        Integer start = (page - 1)* pageSize;

        List<SellerAccountDetails> details = invoiceMapper.qryAccountDetails(sellerId,start,pageSize);

        Integer cnt = invoiceMapper.qryAccountDetailsCnt(sellerId);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("details",details);
        resMap.put("total",cnt);

        return commUtil.returnSuccess(resMap);
    }


    public String qryDetailTotal(String sellerId){
        Double totalCharge = invoiceMapper.qryTotalCharge(sellerId);
        totalCharge=totalCharge==null?0:totalCharge;
        Double totalBill = invoiceMapper.qryTotalBill(sellerId);
        totalBill=totalBill==null?0:totalBill;
//        Integer cnt = invoiceMapper.qryLeftCnt(sellerId);
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("totalCharge",totalCharge);
        resMap.put("totalBill",totalBill);
        //计算剩余金额
        resMap.put("surplusAmt",totalCharge-totalBill);
//        resMap.put("cnt",cnt);

        return commUtil.returnSuccess(resMap);
    }
}
